<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      :inline="true"
      label-width="85px"
    >
      <el-form-item label="姓名" prop="queryName">
        <el-input v-model="formData.queryName" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="身份证号" prop="queryIdCard">
        <el-input v-model="formData.queryIdCard" placeholder="请输入身份证号" />
      </el-form-item>
      <el-form-item v-hasPermi="['business:data-query-lsbg:create']">
        <el-button @click="queryLsbg" type="primary">查询报告{{'（' + lsbgApi.price + '元/次）'}}</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="45px"
    >
      <el-form-item label="姓名" prop="queryName">
        <el-input v-model="queryParams.queryName" placeholder="请输入姓名" />
      </el-form-item>
<!--      <el-form-item label="证件号" prop="queryIdCard">
        <el-input v-model="queryParams.queryIdCard" placeholder="请输入证件号" />
      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      class="mt-10px"
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column
        label="序号"
        header-align="center"
        align="center"
        type="index"
        width="60"
      />
      <el-table-column label="报告编号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="queryName" />
<!--      <el-table-column label="身份证号" align="center" prop="queryIdCard" />-->
      <el-table-column label="报告类型" align="center" prop="reportType">
        <template #default="{row}">
          <p v-if="row.reportType === 1">支付宝</p>
          <p v-else-if="row.reportType === 2">微信</p>
          <p v-else>-</p>
        </template>
      </el-table-column>
      <el-table-column label="邮件格式" align="center" prop="emailFormat">
        <template #default>
          <p>证件材料-PDF</p>
        </template>
      </el-table-column>
      <el-table-column label="发送邮箱" align="center" prop="sendEmail" width="180">
        <template #default>
          <p>w3t3@zhichajinkong.cn</p>
        </template>
      </el-table-column>
      <el-table-column label="页数" align="center" prop="pageSize"/>
      <el-table-column
        label="发送时间"
        align="center"
        prop="queryTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="解析状态" align="center" prop="">
        <template #default>
<!--          <el-tag type="success">解析成功</el-tag>-->
          <p>解析成功</p>
        </template>
      </el-table-column>
<!--      <el-table-column label="流水报告地址" align="center" prop="url" />-->
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="show(scope.row)"
            v-hasPermi="['business:data-query-lsbg:query']"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DataQueryLsbgApi, DataQueryLsbg } from '@/api/business/dataquerylsbg'
import DataQueryLsbgForm from './form/DataQueryLsbgForm.vue'
import {Icon} from "@/components/Icon";
import {ContentWrap} from "@/components/ContentWrap";
import {DataQuery, DataQueryApi} from "@/api/business/dataquery";
import {ApiTypeInfo, ApiTypeInfoApi} from "@/api/business/apitypeinfo";

/** 数据查询-流水报告 列表 */
defineOptions({ name: 'DataQueryLsbg' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DataQueryLsbg[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  deptId: undefined,
  dataId: undefined,
  queryTime: [],
  url: undefined,
  queryName: undefined,
  queryIdCard: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DataQueryLsbgApi.getDataQueryLsbgPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const queryLsbg = async () => {
  // 校验表单
  await formRef.value.validate()

  // 提交请求
  try {
    await DataQueryApi.refreshData({type: 'lsbg', queryName: formData.value.queryName, queryIdCard: formData.value.queryIdCard})
    message.success("查询成功")
    getList()
  } finally {
  }
}

const formData = ref({
  queryName: undefined,
  queryIdCard: undefined,
})
const formRules = reactive({
  queryName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  queryIdCard: [{ required: true, message: '身份证号不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DataQueryLsbgApi.deleteDataQueryLsbg(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const route = useRoute() // 路由
const router = useRouter() // 路由

/** 删除按钮操作 */
const show = async (data) => {
  router.push({
    name: 'DataQueryLsbgForm',
    query: {
      queryName: data.queryName,
      idCard: data.queryIdCard,
      queryTime: data.queryTime,
      queryUrl: data.url,
    }
  })
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: DataQueryLsbg[]) => {
  checkedIds.value = records.map((item) => item.id);
}

const lsbgApi = ref<ApiTypeInfo>({})
/** 查询列表 */
const getLsbgApi = async () => {
  lsbgApi.value = await ApiTypeInfoApi.getApiByCode("lsbg")
}

/** 初始化 **/
onMounted(() => {
  getList()
  getLsbgApi()
})
</script>
